package com.itheima.edu.manager1.controller;

import com.itheima.edu.manager1.domain.Teacher;
import com.itheima.edu.manager1.service.TeachService;

import java.util.Scanner;

public class TeacherController {
    TeachService teachService = new TeachService();
    public void start() {
        Scanner sc = new Scanner(System.in);
      lo:  while(true){
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            switch(choice){
                case"1":
                    //System.out.println("添加老师");
                    addTeacher();
                    break;
                case"2":
                    System.out.println("删除老师");
                    break;
                case"3":
                    System.out.println("修改老师");
                    break;
                case"4":
                   // System.out.println("查看老师");
                    findAllTeacher();
                    break ;
                case"5":
                    System.out.println("感谢使用!");
                    break lo;
                default:
                    System.out.println("您输入有误,请重新输入!");
                    break;
            }
        }
    }

    private void findAllTeacher() {
        //TeachService teachService = new TeachService();    提到了成员变量中
        Teacher[] teachers = teachService.findAllTeacher();
        //2.判断数组的内存地址是否为null
        if(teachers == null){
            System.out.println("查无信息,请重新输入");
            return;
        }
        //3.遍历数组
        System.out.println("学号\t\t姓名\t\t年龄\t\t生日");
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher1 = teachers[i];
            if(teacher1 != null){
                System.out.println(teacher1.getId()+"\t"+teacher1.getName()+"\t\t"+teacher1.getAge()+"\t\t"+teacher1.getBirthday());
            }
        }
    }

    public void addTeacher() {
        String id;
        //1.方法中接收用户信息
        Scanner sc = new Scanner(System.in);
        //接收id后进行判断
        while(true){
            System.out.println("请输入教师职工号:");
            id = sc.next();
            //将id输入到TeachService中进行比较看是否存在
           // TeachService teachService = new TeachService();
            boolean flag = teachService.isExists(id);
            if(flag){
                System.out.println("学号已存在,请重新输入!");
            }else{
                break;
            }
        }

        System.out.println("请输入教师姓名:");
        String name = sc.next();
        System.out.println("请输入教师年龄:");
        String age = sc.next();
        System.out.println("请输入教师生日:");
        String birthday = sc.next();
        //将教师信息封装成对象传递给TeacherService
        Teacher teacher = new Teacher();
        teacher.setId(id);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBirthday(birthday);

        //接受方法的boolean类型返回值根据结果打印成功或添加失败
        TeachService teachService = new TeachService();
        boolean flag = teachService.addTeacher(teacher);
        if(flag){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }
}
